//
//  MessageCenterViewController.m
//  jingjing2.0
//
//  Created by apple on 16/5/22.
//  Copyright © 2016年 apple. All rights reserved.
//
#import "MessageCenterModel.h"
#import "MessageCenterViewController.h"
#import "MessageCenterTableViewCell.h"

@interface MessageCenterViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic,strong)UITableView * mainTableView;
@property (nonatomic,strong)NSMutableArray * dataSourceArray;
@end

@implementation MessageCenterViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = GRAYCOLOR;
    self.title = @"消息中心";
    _dataSourceArray = [NSMutableArray array];
    [self.view addSubview:self.mainTableView];
    
}
-(void)requestTheData
{
    [_dataSourceArray removeAllObjects];
    NSString *timestamp=[GeneralTool gettimestamp];
    
    NSDictionary *dic=@{@"UserCode":GETUSERCODE,@"ClientType":@"2",@"Timestamp":timestamp};
    NSString *sign=[GeneralTool GetSin:dic];
    NSString *urlString=[NSString stringWithFormat:@"Common/MsgCenter/%@/%@/%@/%@",GETUSERCODE,@"2",timestamp,sign];
    [RequestTool requestDataWithType:@"get" andUrlString:urlString andParameters:nil andRequestFinished:^(id responses, NSError *error) {
        if ([responses[@"IsSuccess"]intValue]==1) {
            NSArray * dataArr = [responses objectForKey:@"ReturnObject"];
            for (NSDictionary * dataDic in dataArr) {
                MessageCenterModel * model = [[MessageCenterModel alloc]init];
                [model setValuesForKeysWithDictionary:dataDic];
                [_dataSourceArray addObject:model];
            }
            [_mainTableView reloadData];
        }else
        {
            [MBProgressHUD showError:@"获取信息失败"];
        }
        
    }];
    
}

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [GeneralTool hideTabBar];
    [self requestTheData];
}
-(void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    
    [GeneralTool showTabBar];
}
-(UITableView *)mainTableView
{
    if (!_mainTableView)
    {
        _mainTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0,kWidth, kHeight) style:UITableViewStylePlain];
        _mainTableView.delegate = self;
        _mainTableView.dataSource = self;
        _mainTableView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
        _mainTableView.backgroundColor = GRAYCOLOR;
        _mainTableView.tableFooterView = [UIView new];//设置当数据不多的时候，下面无cell
        [_mainTableView registerClass:[MessageCenterTableViewCell class] forCellReuseIdentifier:@"cell"];
        _mainTableView.sectionHeaderHeight = 0;
        _mainTableView.sectionFooterHeight = 0;
    }
    return _mainTableView;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _dataSourceArray.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    MessageCenterTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    MessageCenterModel * model = _dataSourceArray[indexPath.row];
    [cell.iconImageView sd_setImageWithURL:[NSURL URLWithString:model.IconURL] placeholderImage:nil options:SDWebImageRetryFailed];
    cell.titleLabel.text = model.CategoryName;
    cell.contentLabel.text = model.Remark;
    cell.timeLabel.text = model.RecentTime;
    return cell;
    
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 90;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
